package com.dream.keigezhushou.Activity.acty.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.GlobalConst.GlobalConst;
import com.dream.keigezhushou.Activity.Uiutils.PrefUtils;
import com.dream.keigezhushou.Activity.Uiutils.ToastUtils;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.activity.MainActivity;
import com.dream.keigezhushou.Activity.bean.UserBean;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.R;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Login2Activity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.iv_delete_pass)
    ImageView ivDeletePass;
    @BindView(R.id.tv_forget_pass)
    TextView tvForgetPass;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        ButterKnife.bind(this);

        initData();
    }

    //初始化数据
    protected void initData() {

        ivReturn.setOnClickListener(this);
        ivDeletePass.setOnClickListener(this);
        tvForgetPass.setOnClickListener(this);
        btnLogin.setOnClickListener(this);

    }

    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.iv_delete_pass:

                //删除密码
                etPassword.setText("");
                break;

            case R.id.tv_forget_pass:

                //找回密码
                intent = new Intent(this, FindPassActivity.class);
                startActivity(intent);
                break;

            case R.id.btn_login:

                //登录
                loginRequest();

                break;
        }
    }


    //登录
    private void loginRequest() {



        String phoneNum = etPhone.getText().toString();
        String passWord = etPassword.getText().toString();

                //请求网络进行登录
                OkHttpUtils.post()
                        .url(NetURL.LOGIN)
                        .addParams("mobile",phoneNum)
                        .addParams("password",passWord)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {

                            }

                            @Override
                            public void onResponse(String response) {

                                //请求成功才可以登录

                                //获取到User
                                UserBean user = new Gson().fromJson(response,UserBean.class);

                                String message = user.getMessage();
                                String status = user.getStatus();
                                int state = Integer.parseInt(status);
//                                Log.i("message",message);


                                if (state == 0){

                                    PrefUtils.putBoolean(Login2Activity.this, GlobalConst.PREFUL_ISLOGIN, true); //保存登录状态
                                    intent = new Intent(Login2Activity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }else {

                                    ToastUtils.showToast(Login2Activity.this,message);
                                }



                            }
                        });
    }
}
